<?php
namespace app\admin\controller;
use app\common\model\StoreClassify as StoreClassifyModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;
/**
 * 管理
 * Class Adminstoreclassify
 * @package app\admin\controller
 */
class StoreClassify2 extends AdminBase
{
    protected $storeclassify_model;
    protected function _initialize()
    {
        parent::_initialize();
        $this->storeclassify_model = new StoreClassifyModel();
        $storeclassify_level_list = $this->storeclassify_model->where('pid',0)->order('sort,id DESC')->select();
        $this->assign('storeclassify_level_list',$storeclassify_level_list);
    }
    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $page = 1, $pid='')
    {
        $map = [];
        if ($keyword) {
            $map['name'] = ['like', "%{$keyword}%"];
        }
        if($pid){
            $map['pid'] = $pid;
            $storeclassify_list = $this->storeclassify_model->where($map)->order('sort,id DESC')->paginate(15, false, ['page' => $page]);
        }else{
            $storeclassify_list = $this->storeclassify_model->where($map)->where('pid','neq',0)->order('sort,id DESC')->paginate(15, false, ['page' => $page]);
        }
        return $this->fetch('index', ['storeclassify_list' => $storeclassify_list, 'keyword' => $keyword, 'pid' => $pid]);
    }
    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }
    /**
     * 保存
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'StoreClassify');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->storeclassify_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }
    /**
     * 编辑
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $storeclassify = $this->storeclassify_model->find($id);
        return $this->fetch('edit', ['storeclassify' => $storeclassify]);
    }
    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'StoreClassify');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->storeclassify_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }
    /**
     * 删除
     * @param $id
     */
    public function delete($id = '')
    {
        if($id){
                $fileImg = $this->storeclassify_model->where('id',$id)->value('thumb');
                if($fileImg){
                   if(file_exists('./'.$fileImg)){
                       unlink('./'.$fileImg);
                   }
                }
        }
        if ($id) {
            if ($this->storeclassify_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的内容');
        }
    }
}